package UI;
import java.util.Scanner;
import Crt.*;//The TUILayer needs to know the ControlLayer
import java.util.ArrayList;

/**
 * Write a description of class OrdreUI here.
 * 
 * @author (Gruppe 5) 
 * @version (a version number or a date)
 */
public class OrdreUI
{
    private OrdreCtr oCtr;
    
    /**
     * OrdreUI Constructor
     *
     */
    public OrdreUI()
    {
        oCtr = new OrdreCtr();
    }
    
    /**
     * Method start
     *
     */
    public void start()
    {
        ordreMenu();
    }
    
    /**
     * Method ordreMenu
     *
     * Methoden Starter den valgte methode du ønsker at bruge
     * ved hjælp af nummer du angiver.
     */
    public void ordreMenu()
    {
        boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeOrdreMenu(); 
          if(choice == 1)
          {   //show person
              opretOrdre();
          }
          else{
              if(choice == 2){
                    //create person
                    tilfoejKunde();
              }  
              else{
                  if(choice == 3){
                      //delete person
                      tilfoejVare();
                  }  
                  else{
                         exit = true;
                      }//end else
                  }
              }
       }
    }
    
    /**
     * Method writeOrdreMenu
     *
     * @return The return value
     * 
     * Methoden viser frem hvad du kan vælge på skærmen.
     */
    public int writeOrdreMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Ordre Menu ***");
            System.out.println(" (1) Opret Ordre");
            System.out.println(" (2) tilføj Kunden");
            System.out.println(" (3) tilføj Vare");
            System.out.println(" (4) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    /**
     * Method inputOrdreID
     *
     * @return The return value
     * 
     * Methoden læser den angivene input du har skrevet
     * og tjekker om du kan tilføje den eller om den er null.
     * 
     * Alle input gør det samme men nogle af som inputOrdreID
     * stopper også bogstaver så du kun kan skrive tal.
     */
    public int inputOrdreID()
    {   
        boolean ok = false;
        int oID = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv OrdreID:  ");
           try{
             test = keyboard.nextLine();
             oID = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
           }
        }//end while
        return oID;
    }
    
    public int inputKundeID()
    {   
        boolean ok = false;
        int kID = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv KundeID:  ");
           try{
             test = keyboard.nextLine();
             kID = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
           }
        }//end while
        return kID;
    }
    
    public int inputVareID()
    {   
        boolean ok = false;
        int vID = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv VareID:  ");
           try{
             test = keyboard.nextLine();
             vID = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
           }
        }//end while
        return vID;
    }
    
    public String inputNavn()
    {
        String navn = null;
        boolean ok = false;
        
        while(!ok)
        {
             // makes an object keyboard to have input from the screen
             Scanner keyboard = new Scanner(System.in);  
             System.out.println(" Skriv Navn:  ");
             navn = keyboard.nextLine();
             
             if(navn.matches("[a-åA-Å]+$"))
             {
                 ok = true;
             }
             else
             {
                 System.out.println("Du skal skrive et navn -  prøv igen");
             }
        }
         return navn;
    }
    
    public String inputAdresse()
    {
        String adresse = null;
        boolean ok = false;
        
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv Adresse:  ");
           adresse = keyboard.nextLine();
           
           if(!adresse.equals(""))
           {
             ok = true;
           }
           else
           {
             System.out.println("Du skal skrive en adresse -  prøv igen");
           }
        }//end while
         return adresse;
    }
    
    public String inputTelefon()
    {
        String telefon = null;
        boolean ok = false;
        
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv Telefon nummer:  ");
           telefon = keyboard.nextLine();
           
           if(!telefon.equals(""))
           {
             ok = true;
           }
           else
           {
             System.out.println("Du skal skrive et telefon nummer -  prøv igen");
           }
        }//end while
         return telefon;
    }
    
    public int inputPostNr()
    {   
        boolean ok = false;
        int postNr = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv PostNr:  ");
           try
           {
             test = keyboard.nextLine();
             postNr = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et postNr -  prøv igen");
           }
        }//end while
        return postNr;
    }
    
    public String inputBy()
    {
        String by = null;
        boolean ok = false;
        
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv By:  ");
           by = keyboard.nextLine();
           
           if(by.matches("[a-åA-Å]+$"))
           {
             ok = true;
           }
           else
           {
             System.out.println("Du skal skrive en by -  prøv igen");
           }
        }//end while
         return by;
    }
    
    public String inputMail()
    {
        String mail = null;
        boolean ok = false;
        
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv Mail:  ");
           mail = keyboard.nextLine();
           
           if(!mail.equals(""))
           {
             ok = true;
           }
           else
           {
             System.out.println("Du skal skrive en Mail -  prøv igen");
           }
        }//end while
         return mail;
    }
    
    public int inputAntal()
    {   
        boolean ok = false;
        int antal = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Antal:  ");
           try{
             test = keyboard.nextLine();
             antal = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
           }
        }//end while
        return antal;
    }
    
    /**
     * Method exit
     *
     * Methoden retunere dig til den tidliger menu.
     */
    public void exit()
    {
        System.out.println("\n Press Enter to return");
        
        Scanner button = new Scanner(System.in);
        boolean ok = false;
        while(!ok)
        {
            String theButton = button.nextLine();
            ok = true;
        }
    }
    
    /**
     * Method opretOrdre
     *
     * Methoden bruger det angivne id til at
     * oprette en ny ordre.
     */
    private void opretOrdre()
    {
        int id = inputOrdreID();
        
        oCtr.opretOrdre(id);
    }
    
    /**
     * Method tilfoejKunde
     *
     * Methoden bruger de angivne info til at lave en ny
     * kunde og tilføje kunden til en ordre og KundeContainer.
     */
    private void tilfoejKunde()
    {
        int oID = inputOrdreID();
        int kID = inputKundeID();
        String navn = inputNavn();
        String adresse = inputAdresse();
        String telefon = inputTelefon();
        int postNr = inputPostNr();
        String by = inputBy();
        String mail = inputMail();
        
        oCtr.addKunde(oID, kID, navn, adresse, telefon, postNr, by, mail);
    }
    
    /**
     * Method tilfoejVare
     *
     * Methoden bruger de angivne info til at lave en ny
     * delordre og tilføje en vare og antal til den og så 
     * tilføje den til en ordre.
     */
    private void tilfoejVare()
    {
        int oID = inputOrdreID();
        int vID = inputVareID();
        int antal = inputAntal();
        
        oCtr.addDelOrdre(oID, vID, antal);
    }
}
